/**
 * Store for PositionByClients
 */
Ext.define("Cps.store.PositionByClients",
{
    extend: "Ext.data.Store",
	
	model: 'Cps.model.PositionByClient',
	
	config: {
		params: null,
	},
	
    constructor: function (config)
    {
		if(config)
			this.initConfig(config);
		this.callParent([
		{
			storeId: 'PositionByClients',
			groupers: ['Client','PosCategory'],
			data: []
		}]);
    },
	initData: function() {
		var PositionByClients = this, f_data = [];
		Ext.Ajax.request({
			method: 'GET',
			url: Cps.Config.Api.getPositionByClient,
			params: getParamPosition,
			success: function(data) {
				if (IsJson(data.responseText))
				{
					var data1 = [], total;
					data = Ext.JSON.decode(data.responseText).data;
					data = data ? data.dsPositionByClient : null;
					data1 = data ? data.ttClientList : null;
					data = data ? data.ttPositionByClient : null;
					if (data != undefined)
					{
						if (data.length != undefined)
						{
							for(i = 0; i < data.length; i++) {
								for(j = 0; j < data1.length; j++)
								{
									total = '';
									if (data1[j].ClientId == data[i].ClientId)
									{
										total = data1[j].ClientTotal >= 0 ? Ext.util.Format.number(data1[j].ClientTotal, '0,000.00') : ('-' + Ext.util.Format.number(-data1[j].ClientTotal, '0,000.00'));
										total = total.replace(/[,]/g,"'");
										data[i].Client = data1[j].ClientName + ' - ' + data1[j].ClientCurrency + ' ' + total;
										f_data.push(data[i]);
										break;
									}
								}
							}
						}
						else
							f_data.push(data);
					}
					PositionByClients.loadData(f_data);
					if (Ext.getCmp('mask') != undefined) 
						Ext.getCmp('mask').hide()
				}
			},
			scope: this
		});
	}
});